import { GCtrl } from "../lib/util/GCtrl";
import BtnVCodeSend from "../lib/BtnVCodeSend";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(BtnVCodeSend)
    BtnVCodeSendCom = null;

    @property(cc.Node)
    checkmarkVcode: cc.Node = null;
    @property(cc.Node)
    checkmarkPw: cc.Node = null;
    @property(cc.Node)
    passwordPlane: cc.Node = null;
    @property(cc.Node)
    vcodePlane: cc.Node = null;

    @property(cc.EditBox)
    phoneEdit: cc.EditBox = null;

    @property(cc.EditBox)
    phoneEdit2: cc.EditBox = null;

    @property(cc.EditBox)
    passwordEdit: cc.EditBox = null;

    @property(cc.EditBox)
    vCodeEdit: cc.EditBox = null;


    start() {

    }

    onClickLogin() {

        let client_ip = cc.vv.GLSDKMgr.getClientIp();
        let userinfo = {
            auth_type: "VCODE",
            phone: this.phoneEdit.string,
            vcode: parseInt(this.vCodeEdit.string),
            wechat: "",
            ip: client_ip,
            location: "未知",
            device_type: "未知",
            device_imei: "未知",
            wechat_logo: "",
            wechat_name: "",
            wechat_sex: 1,
        }
        if (cc.sys.isNative) {
            cc.vv.GLSDKMgr.wechatLoginCallback(userinfo);
            this.onClickBack();
        } else {
            cc.vv.GLUtils.getAdress(client_ip, function (address) {
                userinfo["location"] = address;
                userinfo["device_type"] = "安卓";
                userinfo["device_imei"] = cc.vv.GLSDKMgr.getDeviceBrand();
                cc.vv.GLSDKMgr.wechatLoginCallback(userinfo);
                this.onClickBack();

            }.bind(this));
        }

    }


    onClickRegister() {
        cc.vv.UIMgr.open(cc.vv.GLConfig.DWC_BindPhone_path);
        // this.onClickBack();
    }


    onClickBack() {
        cc.vv.UIMgr.close(cc.vv.GLConfig.dwc_PhoneLogin_path, cc.vv.UIMgr.closeType.destory);
    }

    onClickVcode() {
        let sendScoreStr = this.phoneEdit.string;
        if (!this.checkCanTrunNum(sendScoreStr) || sendScoreStr.length !== 11) {
            //提示输入不对
            GCtrl.GLUtils.showTips("输入正确的手机号");
            return;
        }

        cc.vv.GLNet.getHttpVerificationCode(this.phoneEdit.string, () => {
            GCtrl.GLUtils.showTips("发送成功");
            this.BtnVCodeSendCom && this.BtnVCodeSendCom.sendSucessCallBack();
        });
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");

    }


    checkCanTrunNum(str) {
        let can = true;
        for (let i in str) {
            if (str[i] >= '0' && str[i] <= "9") {

            } else {
                return false;
            }
        }
        return can;
    }

    onClickPassword() {
        let str = this.phoneEdit2.string;
        if (!this.checkCanTrunNum(str) || !str) {
            GCtrl.GLUtils.showTips("输入正确的手机号");
            return;
        }

        let client_ip = cc.vv.GLSDKMgr.getClientIp();
        let userinfo = {
            auth_type: "PASSWD",
            phone: this.phoneEdit2.string,
            ip: client_ip,
            login_passwd: this.passwordEdit.string,
        }
        if (cc.sys.isNative) {
            cc.vv.GLSDKMgr.wechatLoginCallback(userinfo);
            this.onClickBack();
        } else {
            cc.vv.GLUtils.getAdress(client_ip, function (address) {
                userinfo["location"] = address;
                userinfo["device_type"] = "安卓";
                userinfo["device_imei"] = cc.vv.GLSDKMgr.getDeviceBrand();
                cc.vv.GLSDKMgr.wechatLoginCallback(userinfo);
                this.onClickBack();

            }.bind(this));
        }
    }

    onClickLoginByPw() {
        this.vcodePlane.active = true;
        this.passwordPlane.active = false;
        this.checkmarkPw.active = false;
        this.checkmarkVcode.active = true;
    }

    onClickLoginByVcode() {

        this.vcodePlane.active = false;
        this.passwordPlane.active = true;
        this.checkmarkPw.active = true;
        this.checkmarkVcode.active = false;
    }
}
